package org.jackie.tomcat.server;

import java.io.IOException;
import java.io.InputStream;

/**
 * Request
 *
 * @author Jackie Hou
 * @date 2023/12/12 16:52
 * @version 1.0
 */
public class Request {

    private InputStream input;
    private String uri;

    public Request(InputStream input) {
        this.input = input;
    }

    public String getUri() {
        return uri;
    }

    public void parse() {
        StringBuffer request = new StringBuffer(2048);
        int i;
        byte[] buffer = new byte[2048];

        try {
            i = input.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            i = -1;
        }

        for (int j=0; j<i; j++) {
            request.append((char)buffer[j]);
        }

        uri = parseUri(request.toString());
        System.out.println("uri = " + uri);
    }

    /**
     * 将输入转换成uri
     * @param requestString
     * @return
     */
    private String parseUri(String requestString) {
        int index1, index2;
        index1 = requestString.indexOf(' ');

        //GET /hello.txt HTTP/1.1
        //截取两个空格中的一段，遵循http协议
        if (index1 != -1) {
            index2 = requestString.indexOf(' ', index1 + 1);
            if (index2 > index1) {
                return requestString.substring(index1 + 1, index2);
            }
        }

        return null;
    }










}
